const envSearch = localStorage.getItem('global-deploy-env') ? localStorage.getItem('global-deploy-env') : 'debug'

const state = {
  env: envSearch,
  envList: [],
  projectList: [],
  projectMap: {},

  resourceList: [],
  resourceMap: {},

  userList: [],
  k8sInstanceList: []
}

const mutations = {
  SET_ENV: (state, env) => {
    state.env = env
    localStorage.setItem('global-deploy-env', env)
  },
  SET_ENV_LIST: (state, envList) => {
    state.envList = envList
  },
  SET_PROJECT_LIST: (state, projectList) => {
    state.projectList = projectList
  },
  SET_PROJECT_MAP: (state, projectMap) => {
    state.projectMap = projectMap
  },
  SET_RESOURCE_LIST: (state, resourceList) => {
    state.resourceList = resourceList
    const pMap = {}
    resourceList.forEach(item => {
      pMap[item.id] = item.name
    })
    state.resourceMap = pMap
  },
  SET_USER_LIST: (state, userList) => {
    state.userList = userList
  },
  SET_K8S_INSTANCE_LIST: (state, k8sInstanceList) => {
    state.k8sInstanceList = k8sInstanceList
  }
}

const actions = {
  setEnv({ commit }, env) {
    commit('SET_ENV', env)
  },
  setEnvList({ commit }, envList) {
    commit('SET_ENV_LIST', envList)
  },
  setProjectList({ commit }, projectList) {
    commit('SET_PROJECT_LIST', projectList)
  },
  setProjectMap({ commit }, projectMap) {
    commit('SET_PROJECT_MAP', projectMap)
  },
  setResourceList({ commit }, resourceList) {
    commit('SET_RESOURCE_LIST', resourceList)
  },
  setUserList({ commit }, userList) {
    commit('SET_USER_LIST', userList)
  },
  setK8sInstanceList({ commit }, k8sInstanceList) {
    commit('SET_K8S_INSTANCE_LIST', k8sInstanceList)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
